package com.leyou.task;

import com.leyou.lock.RedisLock;
import com.leyou.lock.RedisLockImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Slf4j
//@Component
public class CleanOrderTask {

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Scheduled(fixedDelay = 10000)
    public void job4() throws InterruptedException {
        // 初始化锁
        RedisLock lock = new RedisLockImpl(redisTemplate, "lock");
        // 获取锁
        boolean isLock = lock.lock(50);
        // 判断是否获取到了锁
        if(!isLock){
            log.info("获取锁失败，停止定时清理订单");
            return;
        }
        try{
            // 执行我们具体的业务： 清理过期未支付的订单
            log.info("获取锁成功=======>处理订单======>");
            Thread.sleep(5000);
        }catch (Exception e){
            log.error("执行任务出现异常：{}", e.getMessage());
        }finally {
            lock.unlock();
        }
    }

}
